<template>
  <div class="app-container">
    <t-form
      :data="queryParams"
      ref="queryRef"
      layout="inline"
      v-show="showSearch"
      class="mb20"
    >
      <t-form-item label="岗位编码" name="postCode">
        <t-input
          v-model="queryParams.postCode"
          placeholder="请输入岗位编码"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="岗位名称" name="postName">
        <t-input
          v-model="queryParams.postName"
          placeholder="请输入岗位名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="状态" name="status">
        <t-select
          v-model="queryParams.status"
          placeholder="岗位状态"
          clearable
          style="width: 200px"
        >
          <t-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </t-select>
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>

    <t-row :gutter="10" class="mb8">
      <t-col :span="1.5">
        <t-button
          them="primary"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
          >新增
          <template #icon>
            <t-icon name="add" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="success"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:post:edit']"
          >修改
          <template #icon>
            <t-icon name="edit" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="danger"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:post:remove']"
          >删除

          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </t-col>
      <t-col :span="1.5">
        <t-button
          theme="warning"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出
          <template #icon>
            <t-icon name="download" />
          </template>
        </t-button>
      </t-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </t-row>

    <t-table
      v-loading="loading"
      :data="postList"
      row-key="postId"
      :columns="columns"
      @select-change="handleSelectionChange"
    >
      <template #status="scope">
        <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
      </template>
      <template #createTime="scope">
        <span>{{ parseTime(scope.row.createTime) }}</span>
      </template>
      <template #action="scope">
        <t-button
          variant="text"
          theme="primary"
          @click="handleUpdate(scope.row)"
          v-hasPermi="['system:post:edit']"
          >修改
          <template #icon>
            <t-icon name="edit" />
          </template>
        </t-button>
        <t-button
          variant="text"
          theme="primary"
          @click="handleDelete(scope.row)"
          v-hasPermi="['system:post:remove']"
          >删除
          <template #icon>
            <t-icon name="delete" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <t-dialog :title="title" v-model:visible="open" width="500px" attach="body">
      <t-form ref="postRef" :data="form" :rules="rules" label-width="80px">
        <t-form-item label="岗位名称" name="postName">
          <t-input v-model="form.postName" placeholder="请输入岗位名称" />
        </t-form-item>
        <t-form-item label="岗位编码" name="postCode">
          <t-input v-model="form.postCode" placeholder="请输入编码名称" />
        </t-form-item>
        <t-form-item label="岗位顺序" name="postSort">
          <t-input-number
            v-model="form.postSort"
            controls-position="right"
            :min="0"
          />
        </t-form-item>
        <t-form-item label="岗位状态" name="status">
          <t-radio-group v-model="form.status">
            <t-radio
              v-for="dict in sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></t-radio>
          </t-radio-group>
        </t-form-item>
        <t-form-item label="备注" name="remark">
          <t-textarea v-model="form.remark" placeholder="请输入内容" />
        </t-form-item>
      </t-form>
      <template #footer>
        <div class="dialog-footer">
          <t-button theme="primary" @click="submitForm">确 定</t-button>
          <t-button theme="default" @click="cancel">取 消</t-button>
        </div>
      </template>
    </t-dialog>
  </div>
</template>

<script setup name="Post">
import {
  listPost,
  addPost,
  delPost,
  getPost,
  updatePost,
} from "@/api/system/post";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const postList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const columns = ref([
  {
    colKey: "row-select",
    type: "multiple",
    title: "选择",
    width: 50,
    align: "center",
  },
  {
    colKey: "postId",
    title: "岗位编号",
    align: "center",
  },
  {
    colKey: "postCode",
    title: "岗位编码",
    align: "center",
  },
  {
    colKey: "postName",
    title: "岗位名称",
    align: "center",
  },
  {
    colKey: "postSort",
    title: "岗位排序",
    align: "center",
  },
  {
    colKey: "status",
    title: "状态",
    align: "center",
  },
  {
    colKey: "createTime",
    title: "创建时间",
    width: 180,
    align: "center",
  },
  {
    colKey: "action",
    title: "操作",
    width: 280,
    align: "center",
    fixed: "right",
  },
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
    postName: undefined,
    status: undefined,
  },
  rules: {
    postName: [
      { required: true, message: "岗位名称不能为空", trigger: "blur" },
    ],
    postCode: [
      { required: true, message: "岗位编码不能为空", trigger: "blur" },
    ],
    postSort: [
      { required: true, message: "岗位顺序不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  listPost(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined,
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selectedRowKeys, { selectedRowData }) {
  ids.value = selectedRowKeys;
  single.value = selectedRowKeys.length != 1;
  multiple.value = !selectedRowKeys.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加岗位";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || ids.value;
  getPost(postId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改岗位";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate().then((valid) => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPost(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$modal
    .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
    .then(function () {
      return delPost(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
